<template>
  <div>
    <el-table v-loading="loading" :data="tableData" style="width: 100%" @sort-change="sortTable">
      <el-table-column prop="lobbyTypeName" label="类型名" sortable></el-table-column>
      <el-table-column prop="lobbyTypeDescribe" label="类型描述"></el-table-column>
      <el-table-column prop="insertTime" label="添加时间" sortable></el-table-column>
      <el-table-column prop="sort" label="排序" sortable></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="handleEdit(scope)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog  v-loading="ChangeLoading" title="编辑" :visible.sync="dialogVisible" center >
      <el-form :model="editForm" label-width="80px" :rules="rules" ref="form">
        <el-form-item label="类型名" prop="lobbyTypeName">
          <el-input v-model="editForm.lobbyTypeName" ></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="editForm.sort"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer"> <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitChanges('form')">确 定</el-button> </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    // 排序号只能是整数数字
    var validateSort = (rule, value, callback) => {
      let numSort = Number(value);
      if (value.length === 0){
        callback(new Error('请输入排序号'));
      }
      if (isNaN(numSort) || !(numSort === parseInt(value))){
        callback(new Error('只能是整数数字'));
      }
      if (numSort === parseInt(value)){
        callback();
      }
    };
    return {
      loading: true,
      ChangeLoading:true,
      tableData: [],
      tableDateCopy: [],
      dialogVisible: false,
      editForm: {
        id:"",
        lobbyTypeName: "",
        sort: "",
        index:""
      },
      rules: {
        lobbyTypeName: [
          {required: true, message: '影厅类型不能为空', trigger: 'blur'},
        ],
        sort: [{required: true, validator: validateSort, trigger: 'blur' }],
      },
    };

  },
  methods: {
    // 提交修改
    submitChanges(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.ChangeLoading = true;
          axios.post('/manage/upLobbyType', {
            id: this.editForm.id,
            lobbyTypeName: this.editForm.lobbyTypeName,
            sort: this.editForm.sort,
          })
              .then(response => {
                this.ChangeLoading = false;
                let data = response.data;
                // 修改失败
                if (data.code!== 200) {
                  this.$message.error(data.msg);
                  return;
                }
                // 修改成功
                this.$message({
                  message: data.msg,
                  type: 'success'
                });
                this.dialogVisible = false
                this.tableData[this.editForm.index].lobbyTypeName = this.editForm.lobbyTypeName
                this.tableData[this.editForm.index].sort = this.editForm.sort
              })
              .catch(error => {
                this.ChangeLoading = false;
                console.log(error)
                this.$message.error('系统异常，请联系管理员或稍后再试');
              })
              .finally(() => this.loading = false);
        } else {
          this.ChangeLoading = false;
          return false;
        }
      });
    },
    // 编辑
    handleEdit(scope) {
      let row = scope.row;
      this.dialogVisible = true;
      this.editForm.id = row.id;
      this.editForm.lobbyTypeName = row.lobbyTypeName;
      this.editForm.sort = row.sort;
      this.editForm.index = scope.$index
    },
    // 删除
    handleDelete(scope) {
      this.$confirm('确定删除嘛?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios.get('/manage/delLobbyType', {
          params: {
            id: scope.row.id
          }
        }).then(response => {
          if (response.data.code == 200){
            // 丛数组中删除
            this.tableData.splice(scope.$index, 1)
            this.tableDateCopy = [...this.tableData]
            this.$message.success('删除成功');
          }else {
            this.$message.error('删除失败');
          }
        }).catch(error => {
          console.log(error)
          this.$message.error('服务异常，请联系管理员或稍后再试');
        })
      }).catch(() => {});
    },
    // 列表排序
    sortTable({prop, order}) {
      // prop: 排序的字段
      // order: 排序的顺序，ascending/descending/null
      if (order === "ascending") {
        // 升序排序
        this.tableData.sort((a, b) => this.sortName(a[prop], b[prop]));
      } else if (order === "descending") {
        // 降序排序
        this.tableData.sort((a, b) => this.sortName(b[prop], a[prop]));
      } else {
        // 不排序，恢复原始顺序
        this.tableData = [...this.tableDateCopy];
      }
    },
    // 排序
    sortName(a, b) {
      if (a < b) return -1;
      if (a > b) return 1;
      return 0;
    },
    // 获取影厅列表
    getLobbyTypeList() {
      axios.get('/manage/lobbyTypeInfo').then(response => {
        let list = response.data.data;
        console.log(list)
        list.forEach(item => this.tableData.push(item))
        // 拷贝 tableDate 到 tableDateCopy
        this.tableDateCopy = [...this.tableData];
      }).catch(error => {
        console.log(error)
      }).finally(() => this.loading = false)
    }
  },
  created() {
    // 获取初始数据
    axios.get('/manage/lobbyTypeInfo').then(response => {
      let list = response.data.data;
      list.forEach(item => this.tableData.push(item))
      // 拷贝 tableDate 到 tableDateCopy
      this.tableDateCopy = [...this.tableData];
    }).catch(error => {
      console.log(error)
    }).finally(() => this.loading = false)
  }
}
</script>

<style scoped>

</style>